<?php
class NowModule
{
	public function index()
	{
		global $_FANWE;
		$cache_args = array(
			'index',
			$_FANWE['cookie']['city_py'],
			intval($_FANWE['request']['cat']),
			intval($_FANWE['request']['area']),
			trim($_FANWE['request']['sort']),
			trim($_FANWE['request']['prices']),
			$_FANWE['page']
		);

		$cache_file = getTplCache('page/list_index',$cache_args);

		define('IS_CACHE_TPL',true);

		if(!@include($cache_file))
			NowModule::getList();

		display();
	}

	public function today()
	{
		global $_FANWE;

		$cache_args = array(
			'today',
			$_FANWE['cookie']['city_py'],
			intval($_FANWE['request']['cat']),
			intval($_FANWE['request']['area']),
			trim($_FANWE['request']['sort']),
			trim($_FANWE['request']['prices']),
			$_FANWE['page']
		);

		$cache_file = getTplCache('page/list_index',$cache_args);

		define('IS_CACHE_TPL',true);

		if(!@include($cache_file))
		{
			$today_time= getTodayTime();
			$where = " AND g.begin_time > '$today_time'";
			NowModule::getList($where);
		}

		display($cache_file);
	}

	public function custom()
	{
		global $_FANWE;

		define('IS_CUSTOM_SITE',true);

		if($_FANWE['uid'] == 0)
		{
			fHeader("location:".U('user/login'));
			exit;
		}

		$site_ids = S('User')->getCustomSite();

		if(!empty($site_ids))
			NowModule::getList(' AND gk.site_id in ('.$site_ids.')',false,explode(',',$site_ids));
		else
			NowModule::getList('',true);

		display();
	}

	public function search()
	{
		global $_FANWE;

		$page_args = array(
			'city_py' => CURRENT_CITY_PY,
			'sort' => trim($_FANWE['request']['sort']),
			'keyword' => trim($_FANWE['request']['keyword']),
		);

		$keyword = '';
		if(!empty($page_args['keyword']))
		{
			$page_args['keyword'] = urldecode($page_args['keyword']);
			$keyword = &$page_args['keyword'];
			$like_query = mysqlLikeQuote($keyword);
			$keyword_query = clearSymbol($page_args['keyword']);
			if(!empty($keyword_query))
			{
				$search_txt = clearRepeat($keyword_query);
				$keyword_query = segmentToUnicode($keyword_query,'+');
                $fields = ",MATCH (gm.content) AGAINST ('$keyword_query') AS similarity ";
				$where.=" WHERE MATCH (gm.content) AGAINST ('$keyword_query' IN BOOLEAN MODE)";
				$where.=" AND (g.site_name LIKE '%$like_query%' OR g.name LIKE '%$like_query%')";
			}
			else
				$keyword = '';
		}

		if(!empty($keyword))
		{
			$insert_sort = 'gk.cr_sort ASC';

			$city_name = $_FANWE['current_city']['name'];

			if(CURRENT_CITY_PY == CHINA_CITY_PY)
			{
				//全国不包含所有城市商品时
				if($_FANWE['setting']['all_city'] == 0)
				{
					$where.=  " AND gk.city = '$city_name'";
				}
				else
					$insert_sort = 'gk.r_sort ASC';
			}
			elseif(CURRENT_CITY_PY != '')
			{
				//城市显示全国商品时
				if($_FANWE['setting']['index_all'] == 1)
				{
					$fields .= ",(gk.city = '$city_name') as is_now_city";
					$insert_sort = 'is_now_city DESC,'.$insert_sort;
					$quanguo_name = $_FANWE['cache']['city']['all'][CHINA_CITY_PY]['name'];
					$where.=  " AND gk.city IN ('$city_name','$quanguo_name')";
				}
				else
				{
					$where.=  " AND gk.city = '$city_name'";
				}
			}

			$order = '';
			$sort = &$page_args['sort'];
			$sort_array = array('spa'=>'g.shop_price ASC','spd'=>'g.shop_price DESC','eta'=>'gk.end_time ASC','etd'=>'gk.end_time DESC','cca'=>'g.click_count ASC','ccd'=>'g.click_count DESC','sa'=>'g.score ASC','sd'=>'g.score DESC','ba'=>'g.bought ASC','bd'=>'g.bought DESC','da'=>'discount ASC','dd'=>'discount DESC');

			if(array_key_exists($sort,$sort_array))
				$order = $sort_array[$sort];
			else
			{
				unset($page_args['sort']);
				$sort = '';
			}

			if(empty($order))
				$order .= 'similarity DESC,'.$insert_sort.',gk.sort ASC,g.id ASC';
			else
				$order .= ',similarity DESC,'.$insert_sort.',gk.sort ASC,g.id DESC';

			$goods_count = DB::resultFirst('SELECT COUNT(DISTINCT g.id)
				FROM '.DB::table('goods_now_match').' AS gm
				INNER JOIN '.DB::table('goods_now_key').' AS gk ON gk.id = gm.id
				INNER JOIN '.DB::table('goods_now').' AS g ON g.id = gk.id '.$where);

			$pager = buildPage('now/search',$page_args,$goods_count,$_FANWE['page'],24);

			$res = DB::query('SELECT g.id,g.small_img,g.end_time,g.market_price,g.begin_time,g.shop_price,
				gk.city,gk.site_id,g.is_best,g.bought,g.name,g.sort,g.site_name,g.collect_buy,g.click_count,
				g.url,g.collect_count,g.add_time,(g.shop_price / g.market_price) AS discount,g.post_count'.$fields.'
				FROM '.DB::table('goods_now_match').' AS gm
				INNER JOIN '.DB::table('goods_now_key').' AS gk ON gk.id = gm.id
				INNER JOIN '.DB::table('goods_now').' AS g ON g.id = gk.id '.$where.'
				ORDER BY '.$order.' LIMIT '.$pager['limit']);

			$goods_list = array();


			while($goods = DB::fetch($res))
			{
				S('Goods')->goodsFormat($goods);
				$goods_list[] = $goods;
			}
		}

		include template('page/search_index');

		display();
	}

	private function getList($where='',$no_query = false,$site_ids = array())
	{
		global $_FANWE;

		$site_list = $_FANWE['cache']['city_site_'.CURRENT_CITY_PY]['sites'];
		$site_count = count($site_list);

		if($site_count < $_FANWE['setting']['index_site_count'])
		{
			$site_all = $_FANWE['cache']['site']['all'];
			foreach($site_list as $site_id)
			{
				unset($site_all['s_'.$site_id]);
			}

			$site_all = array_slice($site_all,0,$_FANWE['setting']['index_site_count'] - $site_count);
			foreach($site_all as $site)
			{
				$site_list[] = $site['id'];
			}

			unset($site_all,$site,$site_count,$site_id);
		}
		elseif($site_count > $_FANWE['setting']['index_site_count'])
		{
			$site_list = array_slice($site_list,0,$_FANWE['setting']['index_site_count']);
		}

		if(!$no_query)
		{
			if(defined('IS_CACHE_TPL'))
				$fields = '';
			else
				$fields = ',g.post_count';

			$page_args = array(
				'city_py' => CURRENT_CITY_PY,
				'cat' => intval($_FANWE['request']['cat']),
				'area' => intval($_FANWE['request']['area']),
				'sort' => trim($_FANWE['request']['sort']),
				'prices' => trim($_FANWE['request']['prices']),
			);

			$area_where = '';
			$cate_where = '';

			$insert_sort = 'gk.cr_sort ASC';

			if($page_args['cat'] > 0)
				$insert_sort = 'gk.ec_sort ASC';

			if(in_array($page_args['cat'],$_FANWE['cache']['goods']['root_cates']))
				$insert_sort = 'gk.dc_sort ASC';

			$city_name = $_FANWE['current_city']['name'];

			if(CURRENT_CITY_PY == CHINA_CITY_PY)
			{
				//全国不包含所有城市商品时
				if($_FANWE['setting']['all_city'] == 0)
				{
					$where.=  " AND gk.city = '$city_name'";
					$area_where .= " AND ga.city = '$city_name'";
				}
				else
				{
					$insert_sort = 'gk.r_sort ASC';

					if($page_args['cat'] > 0)
						$insert_sort = 'gk.e_sort ASC';

					if(in_array($page_args['cat'],$_FANWE['cache']['goods']['root_cates']))
						$insert_sort = 'gk.d_sort ASC';
				}
			}
			elseif(CURRENT_CITY_PY != '')
			{
				//城市显示全国商品时
				if($_FANWE['setting']['index_all'] == 1)
				{
					$fields .= ",(gk.city = '$city_name') as is_now_city";
					$insert_sort = 'is_now_city DESC,'.$insert_sort;
					$quanguo_name = $_FANWE['cache']['city']['all'][CHINA_CITY_PY]['name'];
					$where.=  " AND gk.city IN ('$city_name','$quanguo_name')";
					$area_where .= " AND ga.city IN ('$city_name','$quanguo_name')";
				}
				else
				{
					$where.=  " AND gk.city = '$city_name'";
					$area_where .= " AND ga.city = '$city_name'";
				}
			}

			//价格条件
			$prices = '';
			if(!empty($page_args['prices']))
			{
				$prices = &$page_args['prices'];
				$price_arr = explode(',',$prices);
				$price_min = intval($price_arr[0]);
				$price_max = intval($price_arr[1]);

				if($price_min > 0)
					$where.=" AND (g.shop_price >= $price_min)";

				if($price_max > 0)
					$where.=" AND (g.shop_price <= $price_max)";
			}
			else
				unset($page_args['prices']);

			$cate_id = 0;
			if($page_args['cat'] > 0)
			{
				$cate_id = &$page_args['cat'];
				$cateobj = $_FANWE['cache']['goods']['cats'][$cate_id];
				if(!empty($cateobj))
				{
					$cate_ids = array();
					if(isset($cateobj['childs']))
						$cate_ids = $cateobj['childs'];

					$cate_ids[] = $cate_id;

					if(count($cate_ids) > 1)
						$cate_where.=' AND gk.cate_id IN ('.implode(',', $cate_ids).')';
					else
						$cate_where.=" AND gk.cate_id = '$cate_id'";
				}
				else
				{
					$cate_id = 0;
					unset($page_args['cat']);
				}
			}
			else
				unset($page_args['cat']);

			if(empty($_FANWE['cache']['city_area_'.CURRENT_CITY_PY]))
				$area_where = '';

			$city_areas = S('Goods')->getGoodsNowArea($where.$cate_where.$area_where);

			$area_id = 0;
			if($page_args['area'] > 0)
			{
				$area_id = &$page_args['area'];
				$areaobj = $_FANWE['cache']['city_area_'.CURRENT_CITY_PY]['all'][$area_id];

				if(!empty($areaobj))
				{
					$area_where.=" AND ga.area = '".$areaobj['name']."'";
				}
				else
				{
					$area_id =0;
                    $area_where = '';
					unset($page_args['area']);
				}
			}
			else
            {
                $area_where = '';
				unset($page_args['area']);
            }

			$goods_cates = S('Goods')->getGoodsNowCats($where.$area_where);

			$order = '';
			$sort = &$page_args['sort'];
			$sort_array = array('spa'=>'g.shop_price ASC','spd'=>'g.shop_price DESC','eta'=>'gk.end_time ASC','etd'=>'gk.end_time DESC','cca'=>'g.click_count ASC','ccd'=>'g.click_count DESC','sa'=>'g.score ASC','sd'=>'g.score DESC','ba'=>'g.bought ASC','bd'=>'g.bought DESC','da'=>'discount ASC','dd'=>'discount DESC');

			if(array_key_exists($sort,$sort_array))
				$order = $sort_array[$sort];
			else
			{
				unset($page_args['sort']);
				$sort = '';
			}

			if(empty($order))
				$order .= 'auction_sort DESC,'.$insert_sort.',gk.sort ASC,g.id ASC';
			else
				$order .= ',auction_sort DESC,'.$insert_sort.',gk.sort ASC,g.id DESC';

			$goods_count = S('Goods')->getGoodsNowCount($where.$cate_where.$area_where);

			$pager = buildPage('now/'.ACTION_NAME,$page_args,$goods_count,$_FANWE['page'],24);

			$query_args = array();
			$query_args['site_ids'] = $site_ids;
			$query_args['is_auction'] = 1;

			$goods_result = S('Goods')->getGoodsNowList($where.$cate_where.$area_where,$order,$fields,$pager['limit'],$query_args);
			$goods_result['ids'] = implode(',',$goods_result['ids']);
		}

		include template('page/list_index');
	}
}
?>